package com.cpi.cornpp.transports.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;

public class DeleteTransportAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";//$NON-NLS-1$
	private static final String FAILURE_FORWARD = "error";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	private static final String TRANSPORT_PURCHASE_ORDER_RELATED_MESSAGE = "info.message.transports.delete.relatedPurchaseOrders";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException{
		String messageError = null;
		TransportForm transportForm = null;
		TransportView transportView = null;
		TransportService transportService = null;
		PurchaseProgramService purchaseProgramService = null;
		List transportPurchaseOrders = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		messageResources = getResources(request);
		
		try{
			
			if (form != null){
				transportForm = (TransportForm) form;
				
				if (StringUtils.isNotEmpty(transportForm.getId())){
					purchaseProgramService = new PurchaseProgramService();
					
					transportPurchaseOrders = purchaseProgramService.getPurchaseOrdersByTransport(Integer.parseInt(transportForm.getId()));
					
					if (CollectionUtils.isEmpty(transportPurchaseOrders)){
						transportService = new TransportService();
						
						transportView = transportService.getTransport(Integer.parseInt(transportForm.getId()));
						
						transportService.removeTransport(transportView);
						
						messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
					} else {
						messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), TRANSPORT_PURCHASE_ORDER_RELATED_MESSAGE);
					}
				}
				
				session.setAttribute(MESSAGE, messageError);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, tre);
				form.reset(mapping, request);
				throw tre;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_LISTS);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return forward;
	}
}